package com.lzx.iteam.net;

import android.os.Message;
import android.util.Log;
import com.lzx.iteam.bean.ChatGroup;
import com.lzx.iteam.bean.CloudContact;
import com.lzx.iteam.util.Constants;
import com.lzx.iteam.util.StringUtil;
import org.json.JSONException;
import org.json.JSONObject;

/**
 * Created by shijunting on 15-1-26.
 */
public class GetInviteContactMsg extends CcMsgStructure {

    private static final String TAG = "GetInviteContactMsg";

    private Message mmCallback;

    public GetInviteContactMsg(Message callback) {
        mmCallback = callback;
    }

    @Override
    public void onReceive(JSONObject data) {
        try {
            JSONObject jsonObject = data.getJSONObject(AsynHttpClient.KEY_CC_DATA);
            CloudContact cloudContact = new CloudContact();
            if (jsonObject.has("contact_id")) {
                cloudContact.contactId = jsonObject.getString("contact_id");
            }
            if (jsonObject.has("group_id")) {
                cloudContact.groupId = jsonObject.getString("group_id");

            }
            if (jsonObject.has("contact_name")) {
                cloudContact.contactName = jsonObject.getString("contact_name");

            }
            if (jsonObject.has("contact_phone")) {
                cloudContact.contact_phone = jsonObject.getString("contact_phone");

            }
            if (mmCallback != null) {
                if ("".equals(cloudContact.contact_phone) || cloudContact.contact_phone == null) {
                    mmCallback.arg1 = 1;
                } else {
                    mmCallback.obj = cloudContact;
                    mmCallback.arg1 = 0;
                }
                mmCallback.sendToTarget();
                return;
            }
        } catch (JSONException e) {
            e.printStackTrace();
        }

    }

    @Override
    public void onError(int error, Object ret) {
        Log.d(TAG, "onError : " + error + " ret: " + ret);
        String errorMsg = null;
        if (ret != null) {
            JSONObject jData = null;
            try {
                jData = new JSONObject(ret.toString());
                errorMsg = jData.getString("msg");
            } catch (JSONException e) {
                errorMsg = "服务端数据解析失败!";
                e.printStackTrace();
            }

        }
        if (mmCallback != null) {
            mmCallback.arg1 = error;
            if (!StringUtil.isEmpty(errorMsg)) {
                mmCallback.obj = errorMsg;
            } else {
                mmCallback.obj = Constants.NETWORK_WRROR;
            }
            mmCallback.sendToTarget();
        }

    }
}
